package com.controller;

import com.pojo.book;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

public class decrServlet extends HttpServlet {

    private final String page1="buy.jsp";
    private final String page2="showServlet";



    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        String decr = req.getParameter("decr");
        String bookid = req.getParameter("bookid");
        String pg = req.getParameter("page");
        int page = Integer.parseInt(pg);

        PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession();
        Map<String, List<book>> cars = (Map<String, List<book>>) session.getAttribute("car");
        String uid=null;
        Cookie[] cookies = req.getCookies();
        for (int i = 0; i < cookies.length; i++) {
            if(cookies[i].getName().equals("loginUser")){
                uid=   cookies[i].getValue();
                break;
            }
        }

        List<book> buycar = cars.get(uid);
        if(buycar==null||buycar.size()<=0){
            req.getRequestDispatcher("/decrFalseServlet").forward(req,resp);
            return;
        }
         
        switch (page) {
            case 1:
                for (int i = 0; i < buycar.size(); i++) {
                    if (buycar.get(i).getBookid().equals(bookid)) {
                        int bookcount = buycar.get(i).getBookcount();

                        if (bookcount - Integer.parseInt(decr) < 0) {
//                    out.print("不能大于原来订购的书本的数量，请重新检查！！");

                            resp.sendRedirect(page1);

                        } else {
                            buycar.get(i).setBookcount(bookcount - Integer.parseInt(decr));
                            cars.put(uid, buycar);
                            session.setAttribute("car", cars);
                            resp.sendRedirect(page1);
                        }
                        break;
                    }
                    break;
                }
                break;
            case 2:
                for (int i = 0; i < buycar.size(); i++) {
                    if (buycar.get(i).getBookid().equals(bookid)) {
                        int bookcount = buycar.get(i).getBookcount();

                        if (bookcount - Integer.parseInt(decr) < 0) {
//                    out.print("不能大于原来订购的书本的数量，请重新检查！！");
                            resp.sendRedirect(page2);
                        } else {
                            buycar.get(i).setBookcount(bookcount - Integer.parseInt(decr));
                            cars.put(uid, buycar);
                            session.setAttribute("car", cars);
                            resp.sendRedirect(page2);
                        }
                        break;
                    }
                    break;

                }
                break;

        }








    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
